package actionScripts.commands
{
	import actionScripts.AdminSetupModelLocator;
	import actionScripts.business.AdminDelegates;
	import actionScripts.common.ClsGlobal;
	import actionScripts.events.AdminSetupDataEvent;
	import actionScripts.events.CommonEvent;
	import actionScripts.utils.AppConstant;
	import actionScripts.utils.Utils;
	import actionScripts.vo.LoggedInUser;
	import actionScripts.vo.UserDetail;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.Responder;

	public class AdminSetupRemoteServiceCalls
	{
		//-------------------------
		// PRIVATE VARIABLES
		//-------------------------
		private var appModelLocator : AdminSetupModelLocator = AdminSetupModelLocator.getInstance();
		private var anEventObject 	: Object;
		public function AdminSetupRemoteServiceCalls(){
		}
		//-------------------------
		// EXECUTE
		//-------------------------
		public function execute( event:AdminSetupDataEvent ) : void {
			var aAdminEvent	    : AdminSetupDataEvent = event;
			anEventObject  		= aAdminEvent.data;
			var aDelegate		: AdminDelegates;
			var aResponder		: Responder;
			var userName 		: String; 
			switch( aAdminEvent.type ) {				
				case AdminSetupDataEvent.GET_ALL_USERS:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( getAllUserResultHandler, getAllUserFaultHandler );
					aDelegate = new AdminDelegates( aResponder );
					aDelegate.getAllUsers();
					break;
				case AdminSetupDataEvent.GET_USERS:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( getUsersResultHandler, getUsersFaultHandler );
					aDelegate = new AdminDelegates( aResponder );
					var userObject :Object = anEventObject as Object;
	    			aDelegate.getUsers(userObject.userName,userObject.firstName,userObject.lastName,userObject.email);
					break;
				case AdminSetupDataEvent.GET_USER_DETAIL:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( getUserDetailResultHandler, getUserDetailFaultHandler );
					aDelegate = new AdminDelegates( aResponder );
					var userId:Number  = anEventObject as Number;
					aDelegate.getUserDetail(userId);
					break;
				case AdminSetupDataEvent.CHECK_USERNAME:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( checkUserResultHandler, checkUserFaultHandler );
					aDelegate = new AdminDelegates( aResponder );
					userName  = anEventObject as String;
					aDelegate.checkUserNameAvailiablity(userName);
					break;
				case AdminSetupDataEvent.UPDATE_USER_DETAIL:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( updateUserDetailResultHandler, updateUserDetailFaultHandler );
					aDelegate = new AdminDelegates( aResponder );
					var userDetail :UserDetail = anEventObject as UserDetail;
					aDelegate.updateUserDetail(userDetail);
					break;
			}		
		}
		
		//------------------------------------------------------------
		// LOGIN RESULT/FAULT HADNLERS
		//------------------------------------------------------------
		public function getAllUserResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var userList:ArrayCollection = event.result as ArrayCollection;
			appModelLocator.userList = userList;
		}
		
		public function getAllUserFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		
		public function getUsersResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var userList:ArrayCollection = event.result as ArrayCollection;
			appModelLocator.userList = userList;
		}
		public function getUsersFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		
		public function getUserDetailResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var usrDetail:UserDetail = event.result as UserDetail;
			appModelLocator.dispatchEvent( new CommonEvent( CommonEvent.GET_USER_DETAIL_EVENT, usrDetail  ) );
		}
		public function getUserDetailFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		public function checkUserResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var isavailiable:Boolean = false;;
			isavailiable = event.result;
			appModelLocator.dispatchEvent( new CommonEvent( CommonEvent.CHECK_USER_EVENT, isavailiable ) );
		}
		public function checkUserFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		
		public function updateUserDetailResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			appModelLocator.dispatchEvent( new CommonEvent( CommonEvent.UPDATE_USER_DETAIL_EVENT, null ) );
		}
		public function updateUserDetailFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		
	}
}